package com.clp.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.lang.Nullable;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 文件类型
 * https://zhuanlan.zhihu.com/p/571208394
 * https://www.dandelioncloud.cn/article/details/1532327177119100929
 */
@Getter
@AllArgsConstructor
public enum FileType {
    _123("00001A00051004", "", "123", "Lotus 1-2-3 spreadsheet (v9) file"),
    _3g2("000000NN66747970336770", "", "3g2", "3rd Generation Partnership Project 3GPP (nn=0x14) and 3GPP2 (nn=0x20) multimedia files"),
    _3gg("000000NN66747970336770", "", "3gg", "3rd Generation Partnership Project 3GPP (nn=0x14) and 3GPP2 (nn=0x20) multimedia files"),
    _3gp("000000NN66747970336770", "", "3gp", "3rd Generation Partnership Project 3GPP (nn=0x14) and 3GPP2 (nn=0x20) multimedia files"),
    _386("4D5A", "", "386", "Executable File"),
    _7z("377ABCAF271C", "", "7z", "7-ZIP compressed file"),

    ACM("4D5A", "", "ACM", "MS audio compression manager driver"),
    ADF("444F53", "", "ADF", "Amiga disk file"),
    AIFF("464F524D00", "", "AIFF", "Audio Interchange File"),
    ANI("52494646", "", "ANI", ""),
    API("4D5A900003000000", "", "API", "Acrobat plug-in"),
    ARJ("60EA27", "", "ARJ", ""),
    ART("4A47030E000000", "", "ART", "AOL ART file"),
    ART_("4A47040E000000", "", "ART", "AOL ART file"),
    AX("4D5A", "", "AX", "Library cache file"),
    AX_("4D5A900003000000", "", "AX", "DirectShow filter"),

    BAS("202020", "", "BAS", ""),
    BMP("424D3E", "", "BMP", ""),
    BZ2("425A68", "", "BZ2", "bzip2 compressed archive"),

    CAB("495363", "", "CAB", ""),
    CAB_("49536328", "", "CAB", "Install Shield v5.x or 6.x compressed file"),
    CAB__("4D534346", "", "CAB", "Microsoft CAB File Format"),
    CBD("434246494C45", "", "CBD", "WordPerfect dictionary file (unconfirmed)"),
    CCD("5B436C", "", "CCD", ""),
    CDR("454C49544520436F6D6D616E64657220", "", "CDR", "Elite Plus Commander saved game file"),
    CDR_("4D535F564F494345", "", "CDR", "Sony Compressed Voice File"),
    CHI("49545346", "", "CHI", "Microsoft Compiled HTML Help File"),
    CHM("49545346", "", "CHM", "Microsoft Compiled HTML Help File"),
    CHM_("495453", "", "CHM", ""),
    CLB("434D5831", "", "CLB", "Corel Binary metafile"),
    CLB_("434F4D2B", "", "CLB", "COM+ Catalog file"),
    COM("4D5A", "", "COM", "Windows/DOS executable file"),
    COM_("4D5AEE", "", "COM", ""),
    COM__("E93B03", "", "COM", ""),
    CPE("464158434F5645522D564552", "", "CPE", "Microsoft Fax Cover Sheet"),
    CPL("4D5A", "", "CPL", "Control panel application"),
    CPT("4350543746494C45", "", "CPT", "Corel Photopaint file"),
    CPT_("43505446494C45", "", "CPT", "Corel Photopaint file"),
    CPX("5B5769", "", "CPX", ""),
    CRU("43525553482076", "", "CRU", "Crush compressed archive"),
    CRW("49491A00000048454150434344520200", "", "CRW", "Canon digital camera RAW file"),
    CTF("436174616C6F6720332E303000", "", "CTF", "WhereIsIt Catalog file"),
    CUR("0000020001002020", "", "CUR", "Windows cursor file"),

    DAT("43524547", "", "DAT", "Windows 9x registry hive"),
    DAT_("436C69656E742055726C4361636865204D4D462056657220", "", "DAT", "IE History DAT file"),
    DAT__("45524653534156454441544146494C45", "", "DAT", "Kroll EasyRecovery Saved Recovery State file"),
    DAT___("496E6E6F20536574757020556E696E7374616C6C204C6F6720286229", "", "DAT", "Inno Setup Uninstall Log file"),
    DB("44424648", "", "DB", "Palm Zire photo database"),
    DDB("000100", "", "DDB", ""),
    DLL("4D5A", "", "DLL", "Windows/DOS executable file"),
    DLL_("4D5A90", "", "DLL", ""),
    DLL__("4D5A90", "", "DLL", ""),
    DMF("00FFFF", "", "DMF", ""),
    DMP("4D444D5093A7", "", "DMP", "Windows minidump file"),
    DMS("444D5321", "", "DMS", "Amiga DiskMasher compressed archive"),
    DPL("4D5A50", "", "DPL", ""),
    DRV("4D5A", "", "DRV", "Windows/DOS executable file"),
    DRV_("4D5A16", "", "DRV", ""),
    DSN("4D56", "", "DSW", "CD Stomper Pro label file"),
    DVF("4D535F564F494345", "", "DV", "Sony Compressed Voice File"),
    DVR("445644", "", "DVR", "DVR-Studio stream file"),

    ECO("2A5052", "", "ECO", ""),
    EML("46726F6D202020", "", "EML", "A commmon file extension for e-mail files. Signatures shown here are for Netscape, Eudora, and a generic signature, respectively. EML is also used by Outlook Express and QuickMail."),
    EML_("46726F6D203F3F3F", "", "EML", "A commmon file extension for e-mail files. Signatures shown here are for Netscape, Eudora, and a generic signature, respectively. EML is also used by Outlook Express and QuickMail."),
    EML__("46726F6D3A20", "", "EML", "A commmon file extension for e-mail files. Signatures shown here are for Netscape, Eudora, and a generic signature, respectively. EML is also used by Outlook Express and QuickMail."),
    EML___("526563", "", "EML", ""),
    Enn("455646", "", "Enn", "EnCase evidence file"),
    EPS("C5D0D3", "", "EPS", ""),
    EVTX("456C6646696C6500", "", "EVTX", "Windows Vista event log file"),
    EXE("4D5A", "", "EXE", "Windows/DOS executable file"),
    EXE_("4D5A90", "", "EXE", ""),

    FLT("4D5A900003000000", "", "FLT", "Audition graphic filter file (Adobe)"),
    FLV("464C5601", "", "FLV", "Flash video file"),
    FNT("434841", "", "FNT", ""),
    FON("4D5A", "", "FON", "Font file"),

    GBC("87F53E", "", "GBC", ""),
    GID("4C4E0200", "", "GID", "Windows Help index file"),
    GIF("47494638", "image/gif", "GIF", ""),
    GTD("7B5072", "", "GTD", ""),
    GX2("475832", "", "GX2", "Show Partner graphics file (not confirmed)"),

    HDMP("4D444D5093A7", "", "HDMP", "Windows heap dump file"),
    HDR("49536328", "", "HDR", "Install Shield v5.x or 6.x compressed file"),
    HLP("3F5F03", "", "HLP", ""),
    HLP_("4C4E0200", "", "HLP", "Windows Help file"),
    HTM("3C2144", "", "HTM", ""),

    IFF("464F524D", "", "IFF", ""),
    IFO("445644", "", "IFO", "DVD info file"),
    IME("4D5A90", "", "IME", ""),
    IMG("00FFFF", "", "IMG", ""),
    IMM("4D5A90", "", "IMM", ""),
    ISO("4344303031", "", "ISO", "ISO-9660 CD Disc Image (This signature usually occurs at byte 8001, 8801, or 9001.)"),

    JAR("4A4152435300", "", "JAR", "JARCS compressed archive"),

    KGB("4B47425F61726368202D", "", "KGB", "KGB archive"),
    KOZ("49443303000000", "", "KOZ", "Sprint Music Store audio file (for mobile devices)"),

    LDB("42494C", "", "LDB", ""),
    LHP("3F5F03", "", "LHP", ""),
    LIB("2A2420", "", "LIB", ""),
    LIT("49544F4C49544C53", "", "LIT", "Microsoft Reader eBook file"),
    LNK("4C0000", "", "LNK", ""),
    LNK_("4C00000001140200", "", "LNK", "Windows shortcut file"),

    MAR("4D41523100", "", "MAR", "Mozilla archive"),
    MAR_("4D415243", "", "MAR", "Microsoft/MSN MARC archive"),
    MAR__("4D41723000", "", "MAR", "MAr compressed archive"),
    MDI("4550", "", "MDI", "Microsoft Document Imaging file"),
    MDS("4D4544", "", "MDS", ""),
    MID("4D546864", "", "MID", "Musical Instrument Digital Interface (MIDI) sound file"),
    MIDI("4D546864", "", "MIDI", "Musical Instrument Digital Interface (MIDI) sound file"),
    MLS("4D494C4553", "", "MLS", "Milestones v1.0 project management and scheduling software (Also see \"MV2C\" and \"MV214\" signatures)"),
    MLS_("4D4C5357", "", "MLS", "Skype localization data file"),
    MLS__("4D56323134", "", "MLS", "Milestones v2.1b project management and scheduling software (Also see \"MILES\" and \"MV2C\" signatures)"),
    MLS___("4D563243", "", "MLS", "Milestones v2.1a project management and scheduling software (Also see \"MILES\" and \"MV214\" signatures)"),
    MMF("4D4D4D440000", "", "MMF", "Yamaha Corp. Synthetic music Mobile Application Format (SMAF) for multimedia files that can be played on hand-held devices."),
    MOV("00000F", "", "MOV", ""),
    MOV_("000077", "", "MOV", ""),
    MP3("494433", "", "MP3", "MPEG-1 Audio Layer 3 (MP3) audio file"),
    MP3_("FFFB50", "", "MP3", ""),
    MPA("000001", "", "MPA", ""),
    MSC("3C3F78", "", "MSC", ""),
    MSV("4D535F564F494345", "", "MSV", "Sony Memory Stick Compressed Voice file"),

    NES("4E4553", "", "NES", ""),
    NLS("C22020", "", "NLS", ""),

    OCX("4D5A90", "", "OCX", ""),
    OCX_("4D5A", "", "OCX", "ActiveX or OLE Custom Control"),
    OLB("4D5A90", "", "OLB", ""),
    OLB_("4D5A", "", "OLB", "OLE object library"),

    PAT_("47504154", "", "PAT", "GIMP (GNU Image Manipulation Program) pattern file"),
    PBK("5B4144", "", "PBK", ""),
    PCB("17A150", "", "PCB", ""),
    PCS("0A0501", "", "PCS", ""),
    PDF("255044", "", "PDF", ""),
    PDG("484802", "", "PDG", ""),
    PIF("4D5A", "", "PIF", "Windows/DOS executable file"),
    PAT("4746315041544348", "", "PAT", "Advanced Gravis Ultrasound patch file"),
    PJT("000007", "", "PJT", ""),
    PLL("24536F", "", "PLL", ""),
    PNG("89504E47", "", "PNG", ""),
    PNG_("89504E", "", "PNG", ""),
    PPC("526563", "", "PPC", ""),
    PPT("D0CF11", "", "PPT", ""),
    PPZ("4D534346", "", "PPZ", "Powerpoint Packaged Presentation"),
    PRG("234445", "", "PRG", ""),
    PSD("384250", "", "PSD", ""),

    QTS("4D5A", "", "QTS", "Windows/DOS executable file"),
    QTX("4D5A", "", "QTX", "Windows/DOS executable file"),

    RAR("526175", "", "RAR", ""),
    RAW("060500", "", "RAW", ""),
    RM("2E524D", "", "RM", ""),
    RTD("43232B44A4434DA5486472", "", "RTD", "RagTime document file"),
    RTF("7B5C72", "", "RTF", ""),

    SBV("46454446", "", "SBV", "(Unknown file type)"),
    SCH("2A7665", "", "SCH", ""),
    SCR("4D5A", "", "SCR", "Screen saver"),
    SH3("4848474231", "", "SH3", "Harvard Graphics presentation file"),
    SHD("4B490000", "", "SHD", "Windows 9x printer spool file"),
    SMD("00FFFF", "", "SMD", ""),
    SNP("4D534346", "", "SNP", "Microsoft Access Snapshot Viewer file"),
    SUB("FFFFFF", "", "SUB", ""),
    SWF("465753", "", "SWF", "Macromedia Shockwave Flash player file"),
    SWF_("435753", "", "SWF", "Shockwave Flash file (v5+)"),
    SYS("4D5A", "", "SYS", "Windows/DOS executable file"),

    TAG("000002", "", "TAG", ""),
    TARBZ2("425A68", "", "TAR.BZ2", "bzip2 compressed archive"),
    TB2("425A68", "", "TB2", "bzip2 compressed archive"),
    TBZ2("425A68", "", "TBZ2", "bzip2 compressed archive"),
    TGA("000002", "", "TGA", ""),
    TIF("492049", "", "TIF", "Tagged Image File Format file"),
    TIF_("4D4D002A", "", "TIF", "Tagged Image File Format file (big endian, i.e., LSB last in the byte; Motorola)"),
    TIF__("4D4D002B", "", "TIF", "BigTIFF files; Tagged Image File Format files >4 GB"),
    TIFF("492049", "", "TIFF", "Tagged Image File Format file"),
    TIFF_("4D4D002A", "", "TIFF", "Tagged Image File Format file (big endian, i.e., LSB last in the byte; Motorola)"),
    TIFF__("4D4D002B", "", "TIFF", "BigTIFF files; Tagged Image File Format files >4 GB"),
    TLB("4D53465402000100", "", "TLB", "OLE, SPSS, or Visual C++ type library file"),
    TST("000100", "", "TST", ""),
    TTF("000100", "", "TTF", ""),

    VBX("4D5A", "", "VBX", "VisualBASIC application"),
    VCD("454E5452595643440200000102001858", "", "VCD", "VideoVCD (GNU VCDImager) file"),
    VXD("4D5A", "", "VXD", "Windows virtual device drivers"),

    WAV("524946", "", "WAV", ""),
    WMA("3026B2", "", "WMA", ""),
    WMV("3026B2", "", "WMV", ""),
    WRI("31BE00", "", "WRI", ""),

    XBE("584245", "", "XBE", ""),
    XLS("D0CF11", "", "XLS", ""),
    XML("3C3F78", "", "XML", ""),
    XMV("005001", "", "XMV", ""),
    XSL("FFFE3C", "", "XSL", ""),

    Z("1F9D8C", "", "Z", ""),
    ZIP("504B03", "", "ZIP", ""),
    Zip("504B3030504B0304", "", "Zip", "WINZIP Compressed"),

    aba("00014241", "", "aba", "Palm Address Book Archive file"),
    abi("414F4C494E444558", "", "abi", "AOL address book index file"),
    aby("414F4C4442", "", "aby", "AOL database files: address book (ABY) and user configuration data (MAIN.IDX)"),
    accdb("000100005374616E6461726420414345204442", "", "accdb", "Microsoft Access 2007 file"),
    acm("4D5A", "", "acm", "Executable File"),
    adx("0300000041505052", "", "adx", "Lotus Approach ADX file"),
    ain("2112", "", "ain", "AIN Compressed Archive File"),
    ami("5B7665725D", "", "ami", "Lotus Ami Pro"),
    amr("2321414D52", "", "amr", "Adaptive Multi-Rate ACELP (Algebraic Code Excited Linear Prediction) Codec, commonly audio format with GSM cell phones"),
    apr("D0CF11E0A1B11AE1", "", "apr", "MS Compound Document v1 or Lotus Approach APR file"),
    arc("1A0x", "", "arc", "LH archive file, old version(where x = 0x2, 0x3, 0x4, 0x8 or 0x9 for types 1-5, respectively)"),
    arc_("41724301", "", "arc", "FreeArc compressed file"),
    arj("60EA", "", "arj", "ARJ Compressed Archive"),
    asf("3026B2758E66CF11", "", "asf", "Windows Media"),
    asf_("3026B2758E66CF11A6D900AA0062CE6C", "", "asf", "Microsoft Windows Media Audio/Video File(Advanced Streaming Format)"),
    asx("3C", "", "asx", "Advanced Stream redirector file"),
    au("2E736E64", "audio/basic", "au", "SoundMachine Audio FileNeXT/Sun Microsystems μ-Law audio file"),
    avi("41564920", "video/x-msvideo", "avi", "Audio Video Interleave (AVI)"),
    ax("4D5A", "", "ax", "Executable File"),

    bag("414F4C2046656564626167", "", "bag", "AOL and AIM buddy list file"),
    bin("424C4932323351", "", "bin", "Thomson Speedtouch series WLAN router firmware"),
    bmp("424D", "image/bmp", "bmp", "Windows Bitmap"),
    bz("425A68", "", "bz", "BZIP Archive"),
    bz2("425A68", "", "bz2", "BZIP Archive"),

    cat("30", "", "cat", "Microsoft security catalog file"),
    cdr("CDR", "", "cdr", "Corel Draw"),
    cnt("3A42617365", "", "cnt", ""),
    com("4D5A", "", "com", "Executable File"),
    cpio("303730373037", "", "cpio", "CPIO Archive File"),
    cpl("4D5A", "", "cpl", "Executable File"),
    cru("43525553482076", "", "CRU", "CRUSH Archive File"),
    crush("4352555348", "", "crush", "CRUSH Archive File"),

    dat("03", "", "dat", "MapInfo Native Data Format"),
    dat_("1A52545320434F4D5052455353454420494D4147452056312E301A", "", "dat", "Runtime Software disk image"),
    dat__("415647365F496E746567726974795F4461746162617365", "", "dat", "AVG6 Integrity database file"),
    db("0006156100000002000004D200001000", "", "db", "Netscape Navigator (v4) database file"),
    db_("08", "", "db", "dBASE IV or dBFast configuration file"),
    db__("D0CF11E0", "", "db__", "MS Office/OLE2"),
    db___("D0CF11E0A1B11AE1", "", "db", "MS Compound Document v1 or Lotus Approach APR file"),
    db3("03", "", "db3", "dBASE III file"),
    db4("04", "", "db4", "dBASE IV data file"),
    dba("00014244", "", "dba", "Palm DateBook Archive file"),
    dbx("CFAD12FEC5FD746F", "", "dbx", "Outlook Express"),
    dbx_("CFAD12FE", "", "dbx", ""),
    dci("3C21646F63747970", "", "dci", "AOL HTML mail file"),
    dcx("3ADE68B1", "", "dcx", "DCX Graphic File"),
    dib("424D", "", "dib", "device-independent bitmap image"),
    dll("4D5A", "", "dll", "Executable File"),
    doc("D0CF11E0A1B11AE1", "", "doc", "MS Compound Document v1 or Lotus Approach APR file"),
    doc_("1234567890FF", "", "doc", "MS Word 6.0"),
    doc__("31BE000000AB0000", "", "doc", "MS Word for DOS 6.0"),
    doc___("7FFE340A", "", "doc", "MS Word"),
    doc____("0D444F43", "", "doc", "DeskMate Document file"),
    docx("504B0304", "", "docx", ""),
    dot("D0CF11E0", "", "dot", "MS Office/OLE2"),
    dot_("D0CF11E0A1B11AE1", "", "doc", "MS Compound Document v1 or Lotus Approach APR file"),
    drv("4D5A", "", "drv", "Executable File"),
    drw("07", "", "drw", "A common signature and file extension for many drawing programs."),
    drw_("01FF02040302", "", "drw", "Micrografx vector graphic file"),
    ds4("4D47582069747064", "", "ds4", "Micrografix Designer 4"),
    dsp("23204D6963726F736F667420446576656C6F7065722053747564696F", "", "dsp", "Microsoft Developer Studio project file"),
    dss("02647373", "", "dss", "Digital Speech Standard (Olympus, Grundig, & Phillips)"),
    dtd("0764743264647464", "", "dtd", "DesignTools 2D Design file"),
    dtd_("3C21454E54495459", "", "dtd", "XML DTD"),
    dwg("41433130", "", "dwg", "Generic AutoCAD drawingNOTES on AutoCAD file headers: The 0x41-43-31-30 (AC10) is a generic header, occupying the first four bytes in the file. The next two bytes give further indication about the version or subtype:0x30-32 (02) — AutoCAD R2.50x30-33 (03) — AutoCAD R2.60x30-34 (04) — AutoCAD R90x30-36 (06) — AutoCAD R100x30-39 (09) — AutoCAD R11/R120x31-30 (10) — AutoCAD R13 (subtype 10)0x31-31 (11) — AutoCAD R13 (subtype 11)0x31-32 (12) — AutoCAD R13 (subtype 12)0x31-33 (13) — AutoCAD R14 (subtype 13)0x31-34 (14) — AutoCAD R14 (subtype 14)0x31-35 (15) — AutoCAD R20000x31-38 (18) — AutoCAD R20040x32-31 (21) — AutoCAD R2007"),
    dwg_("414331", "", "dwg", ""),

    elf("7F454C4601010100", "", "elf", "ELF Executable"),
    emf("0100000058000000", "", "emf", "Extended (Enhanced) Windows Metafile Format, printer spool file"),
    eml("44656C69766572792D646174653A", "", "eml", "Email"),
    enc("005C41B1FF", "", "enc", "Mujahideen Secrets 2 encrypted file"),
    enl("40404020000040404040", "", "enl", "EndNote Library File"),
    eps("252150532D41646F6265", "", "eps", "Postscript"),
    eps_("25215053", "", "eps", "Adobe EPS File"),
    eps__("252150532D41646F62652D332E3020455053462D332030", "", "eps", "Adobe encapsulated PostScript file (If this signature is not at the immediate beginning of the file, it will occur early in the file, commonly at byte offset 30)"),
    eth("1A350100", "", "eth", "GN Nettest WinPharoah capture file"),
    evt("300000004C664C65", "", "evt", "Windows Event Viewer file"),
    evt_("03000000C466C456", "", "evt", ""),
    exe("4D5A", "", "exe", "Executable File"),

    fdf("25504446", "application/fdf", "fdf", "Adobe Portable Document Format and Forms Document file"),
    fli("0011AF", "", "fli", "FLIC Animation file"),
    flt("4D5A", "", "flt", "Executable File"),
    flt_("000101", "", "flt", "OpenFlight 3D file"),
    flv("464C5601", "", "flv", "Flash video file"),
    fm("3C4D616B657246696C6520", "", "fm", "Adobe FrameMaker file"),
    fm3("00001A0007800100", "", "fm3", "Lotus 123 v3 FMT file"),
    fmt("20006800200", "", "fmt", "Lotus 123 v4 FMT file"),
    fon("4D5A", "", "fon", "Executable File"),

    gid("3F5F0300", "", "gid", "Windows Help Index File"),
    gif("474946383761", "image/gif", "gif", "Graphics interchange format file (GIF 87A)"),
    gif_("474946383961", "", "gif", "Graphics interchange format file (GIF89A)"),
    gz("1F8B", "application/x-gzip", "gz", "Gzip Archive File"),
    gz_("1F8808", "", "gz", "GZ Compressed File"),

    hap("91334846", "", "hap", "HAP Archive File"),
    hdr("233F52414449414E43450A", "", "hdr", "adiance High Dynamic Range image file"),
    hlp("3F5F0300", "", "hlp", "Windows Help file"),
    hlp_("0000FFFFFFFF", "", "hlp", "Windows Help file"),
    hqx("28546869732066696C65", "", "hqx", "Macintosh BinHex 4 Compressed Archive"),
    hqx_("28546869732066696C65206D75737420626520636F6E76657274656420776974682042696E48657820", "", "hqx", "Macintosh BinHex 4 Compressed Archive"),
    htm("3C48544D4C3E", "", "htm", "HyperText Markup Language 2"),
    htm_("3C21444F4354", "", "htm", "HyperText Markup Language 3"),
    htm__("3C68746D6C3E", "", "htm", "HyperText Markup Language 1"),
    html("68746D6C3E", "text/html", "html", "HTML"),
    html_("3C68746D6C3E", "", "html", "HyperText Markup Language 1"),
    html__("3C48544D4C3E", "", "html", "HyperText Markup Language 2"),
    html___("3C21444F4354", "", "html", "HyperText Markup Language 3"),

    ico("0000010000", "", "ico", "Icon File"),
    ico_("0000010001002020", "", "ico", "Icon File"),
    idx("414F4C4442", "", "idx", "AOL database files: address book (ABY) and user configuration data (MAIN.IDX)"),
    img("000100080001000101", "", "img", "Ventura Publisher/GEM VDI Image Format Bitmap file"),
    ind("414F4C494458", "", "ind", "AOL client preferences/settings file (MAIN.IND)"),
    ivr("2E524543", "", "ivr", "RealPlayer video file (V11 and later)"),

    jar("5F27A889", "", "jar", "JAR Archive File"),
    jar_("504B0304", "", "jar", "ZIP Archive"),
    jpe("FFD8FFE000", "", "jpe", "JPG Graphic File"),
    jpe_("FFD8FFFE00", "", "jpe", "JPG Graphic File"),
    jpeg("FFD8FFE000", "image/jpeg", "jpeg", "JPG Graphic File"),
    jpeg_("FFD8FFFE00", "", "jpeg", "JPG Graphic File"),
    jpg("FFD8FF", "image/jpg", "jpg", ""),
    jpg_("FFD8FFE000", "", "jpg", "JPG Graphic File"),
    jpg__("FFD8FFFE00", "", "jpg", "JPG Graphic File"),

    lha("2D6C68352D", "", "lha", "LHA Compressed"),
    lha_("2D6C68", "", "lha", "Compressed archive file"),
    lhp("3F5F0300", "", "lhp", "Windows Help File"),
    lib("213C617263683E0A", "", "lib", "Unix archiver (ar) files and Microsoft Program Library Common Object File Format (COFF)"),
    lnk("4C000000", "", "lnk", "Windows Shortcut (Link File)"),
    lnk_("4C000000011402", "", "lnk", "Windows Link File"),
    log("2A2A2A2020496E7374616C6C6174696F6E205374617274656420", "", "log", "Symantec Wise Installer log file"),
    lrc("4D5A", "", "lrc", "Executable File"),
    lwp("576F726450726F", "", "lwp", "Lotus WordPro v9"),
    lzh("2D6C68", "", "lzh", "Compressed archive file"),
    lzh_("lh", "", "lzh", "Lz compression file"),

    m3u("234558", "", "m3u", ""),
    m4a("00000020667479704D34412000000000", "", "m4a", "QuickTime M4A/M4V file"),
    m4v("00000020667479704D34412000000000", "", "m4v", "QuickTime M4A/M4V file"),
    manifest("3C3F786D6C2076657273696F6E3D", "", "manifest", "Windows Visual Stylesheet XML file"),
    max("D0CF11", "", "max", ""),
    mda("5374616E64617264204A", "", "mda", "MS Access"),
    mdb("5374616E64617264204A", "", "mdb", "MS Access"),
    mdb_("000100005374616E64617264204A6574204442", "", "mdb", "Microsoft Access file"),
    mde("5374616E64617264204A", "", "mde", "MS Access"),
    mdf("00FFFFFFFFFFFFFFFFFFFF0000020001", "", "mdf", "Alcohol 120% CD image"),
    mdf_("010F0000", "", "mdf", "Microsoft SQL Server 2000 database"),
    mdt("5374616E64617264204A", "", "mdt", "MS Access"),
    mid("4D546864", "audio/midi", "mid", "Musical Instrument Digital Interface (MIDI) sound file"),
    midi("4D546864", "audio/midi", "midi", "Musical Instrument Digital Interface (MIDI) sound file"),
    mkv("1A45DFA3934282886D6174726F736B61", "", "mkv", "Matroska stream file"),
    mny("000100004D534953414D204461746162617365", "", "mny", "Microsoft Money file"),
    mov("6D6F6F76", "", "mov", "Quicktime"),
    mov_("6D646174", "", "mov", "QuickTime Movie"),
    mp("0CED", "", "mp", "Monochrome Picture TIFF bitmap file (unconfirmed)"),
    mp3("494433", "", "mp3", "MPEG-1 Audio Layer 3 (MP3) audio file"),
    mp3_("FFFB50", "", "mp3", ""),
    mp4("000000186674797033677035", "", "mp4", "MPEG-4 video files"),
    mpeg("000001B3", "video/mpeg", "mpeg", "MPEG Movie"),
    mpg("000001B3", "video/mpeg", "mpg", "MPEG Movie"),
    mpg_("000001BA", "", "mpg", "MPEG"),
    msc("3C3F786D6C2076657273696F6E3D22312E30223F3E0D0A3C4D4D435F436F6E736F6C6546696C6520436F6E736F6C6556657273696F6E3D22", "", "msc", "Microsoft Management Console Snap-in Control file"),
    msi("D0CF11E0", "", "msi", "MS Office/OLE2"),
    msi_("D0CF11E0A1B11AE1", "", "msi", "MS Compound Document v1 or Lotus Approach APR file"),
    msi__("2320", "", "msi", "Cerius2 file"),

    nri("0E4E65726F49534F", "", "nri", "Nero CD Compilation"),
    nsf("1A0000030000", "", "nsf", "Lotus Notes Database/Template"),
    nsf_("1A00000300001100", "", "nsf", "Notes Database"),
    nsf__("1A0000040000", "", "nsf", "Lotus Notes database"),
    ntf("1A0000", "", "ntf", "Lotus Notes database template"),
    ntf_("1A0000030000", "", "ntf", "Lotus Notes Database/Template"),
    ntf__("30314F52444E414E43452053555256455920202020202020", "", "ntf", "National Transfer Format Map File"),

    obj("4C01", "", "obj", "Microsoft Common Object File Format (COFF) relocatable object code file for an Intel 386 or later/compatible processors"),
    ocx("4D5A", "", "ocx", "Executable File"),
    org("414F4C564D313030", "", "org", "AOL personal file cabinet (PFC) file"),

    pak("1A0B", "", "pak", "Compressed archive file"),
    pcx("0Ann0101", "", "pcx", "ZSOFT Paintbrush file(where nn = 0x02, 0x03, or 0x05)"),
    pcx_("0A050108", "", "pcx", "PC Paintbrush(often associated with Quake Engine games)"),
    pdb("000000000000000000000000000000000000000000000000", "", "pdb", "Palmpilot Database/Document File"),
    pdf("255044462D312E", "", "pdf", "Adobe Acrobat"),
    pdf_("25504446", "", "pdf", "Adobe Portable Document Format and Forms Document file"),
    pf("1100000053434341", "", "pf", "Windows prefetch file"),
    pfc("414F4C564D313030", "", "pfc", "AOL personal file cabinet (PFC) file"),
    pic("0100000001", "", "pic", "Unknown type picture file"),
    png("89504E470D0A", "image/png", "png", "PNG Image File"),
    png_("89504E470D0A1A0A", "", "png", "PNG Image File"),
    pot("D0CF11E0", "", "pot", "MS Office/OLE2"),
    pot_("D0CF11E0A1B11AE1", "", "pot", "MS Compound Document v1 or Lotus Approach APR file"),
    ppa("D0CF11E0", "", "ppa", "MS Office/OLE2"),
    ppa_("D0CF11E0A1B11AE1", "", "ppa", "MS Compound Document v1 or Lotus Approach APR file"),
    pps("D0CF11E0", "", "pps", "MS Office/OLE2"),
    pps_("D0CF11E0A1B11AE1", "", "pps", "MS Compound Document v1 or Lotus Approach APR file"),
    ppt("D0CF11E0", "", "ppt", "MS Office/OLE2"),
    ppt_("D0CF11E0A1B11AE1", "", "ppt", "MS Compound Document v1 or Lotus Approach APR file"),
    ppt__("006E1EF0", "", "ppt", "PowerPoint presentation subheader (MS Office)"),
    ppt___("0F00E803", "", "ppt", "PowerPoint presentation subheader (MS Office)"),
    pptx("504B030414", "", "pptx", ""),
    prc("424F4F4B4D4F4249", "", "prc", "Palmpilot resource file"),
    ps("252150532D41646F62", "", "ps", "Postscript"),
    psd("38425053", "", "psd", "Adobe Photoshop image file"),
    psp("7E424B00", "", "psp", "PaintShop Pro Image File"),
    pst("2142444E", "", "pst", "Microsoft Outlook Personal Folder file"),
    pwl("E3828596", "", "pwl", "Windows Password"),

    qbb("458600000600", "", "qbb", "Intuit QuickBooks Backup file"),
    qdf("AC9EBD8F", "", "qdf", "Quicken"),
    qph("03000000", "", "qph", "Quicken price history file"),
    qt("6D646174", "", "qt", "Quicktime Movie File"),
    qxd("00004949585052", "", "qxd", "Quark Express document (Intel & Motorola, respectively)"),
    qxd_("00004D4D585052", "", "qxd", ""),

    ra("2E524D460000001200", "audio/x-pn-realaudio", "ra", "Real Audio file"),
    ra_("2E7261FD", "", "ra", "Real Audio File"),
    ra__("2E7261FD00", "", "ra", "RealAudio streaming media file"),
    ram("2E7261FD", "audio/x-pn-realaudio", "ram", "Real Audio File"),
    rar("52617221", "", "rar", "RAR Archive File"),
    reg("5245474544495434", "", "reg", ""),
    rgb("01DA01010003", "", "rgb", "Silicon Graphics RGB Bitmap"),
    rm("2E524D46", "", "rm", "Real Media streaming media file"),
    rmvb("2E524D46", "", "rmvb", "Real Media streaming media file"),
    rpm("EDABEEDB", "", "rpm", "RPM Archive File"),
    rtf("7B5C727466", "application/rtf", "rtf", "Rich Text Format File"),

    sav("24464C3240282329205350535320444154412046494C45", "", "sav", "SPSS Data file"),
    scr("4D5A", "", "scr", "Executable File"),
    scm("805343", "", "scm", ""),
    sdw("D0CF11E0", "", "sdw", "MS Office/OLE2"),
    sdw_("D0CF11E0A1B11AE1", "", "sdw", "MS Compound Document v1 or Lotus Approach APR file"),
    sit("53495421", "", "sit", "Stuffit v1 Archive File"),
    slt("53520100", "", "slt", "Sage sly.or.srt.or.slt"),
    sit_("53747566664974", "", "sit", "Stuffit v5 Archive File"),
    sle("3A56455253494F4E", "", "sle", "Surfplan kite project file"),
    sle_("414376", "", "sle", "teganos Security Suite virtual secure drive"),
    sly("53520100", "", "sly", "Sage sly.or.srt.or.slt"),
    snm("001D849000000000", "", "snm", "Netscape Communicator (v4) mail folder"),
    sol("00BF", "", "sol", "Adobe Flash shared object file (e.g., Flash cookies)"),
    spl("00000100", "", "spl", "Windows NT/2000/XP printer spool file"),
    srt("53520100", "", "srt", "Sage sly.or.srt.or.slt"),
    swf("465753", "", "swf", "Macromedia Shockwave Flash player file"),
    sxc("calc", "", "sxc", "OpenOffice Calc"),
    sxd("draw", "", "sxd", "OpenOffice Draw"),
    sxi("impress", "", "sxi", "OpenOffice Impress"),
    sxm("math", "", "sxm", "OpenOffice Math"),
    sxw("writer", "", "sxw", "OpenOffice Writer"),
    sys("4D5A", "", "sys", "Executable File"),
    syw("414D594F", "", "syw", "Harvard Graphics symbol graphic"),

    tar("1F8B", "application/x-tar", "tar", "Gzip Archive File"),
    tar_("303730373037", "", "tar", "CPIO Archive File"),
    tarz("1F9D90", "", "tar", "Compressed tape archive file"),
    tga("0000100000", "", "tga", "RLE压缩的前5字节"),
    tga_("0000020000", "", "tga", "未压缩的前5字节"),
    tgz("1F8B", "", "tgz", "Gzip Archive File"),
    tgz_("1F8808", "", "tgz", "GZ Compressed File"),
    tif("49492A00", "", "tif", "Tagged Image File Format file (little endian, i.e., LSB first in the byte; Intel)"),
    tif_("49492A", "", "tif", "TIFF (Intel)"),
    tif__("4D4D2A", "", "tif", "TIFF (Motorola)"),
    tiff("49492A", "", "tiff", "TIFF (Intel)"),
    tiff_("49492A00", "", "tiff", "Tagged Image File Format file (little endian, i.e., LSB first in the byte; Intel)"),
    tiff__("4D4D2A", "", "tiff", "TIFF (Motorola)"),
    tr1("0110", "", "tr1", "Novell LANalyzer capture file"),
    txt("EFBBBF", "", "txt", "UTF-8 txt"),
    txt_("FEFF", "", "txt", "Unicode txt"),
    txt__("FFFE", "", "txt", "Unicode big endian"),

    ufa("554641", "", "ufa", "UFA Archive File"),

    vbx("4D5A", "", "vbx", "Executable File"),
    vcf("424547494E3A56434152440D0A", "", "vcf", "vCard file"),
    vob("000001BA", "", "vob", "DVD Video Movie File (video/dvd, video/mpeg)"),
    vxd("4D5A", "", "vxd", "Executable File"),

    wav("57415645", "", "wav", "Wave"),
    wav_("57415645666D74", "", "wav", "Wave Files"),
    wb2("00000200", "", "wb2", "QuattroPro for Windows Spreadsheet file"),
    wb3("3E000300FEFF090006", "", "wb3", "Quatro Pro for Windows 7.0 Notebook file"),
    wk1("2000604060", "", "wk1", "Lotus 123 v1 Worksheet"),
    wk1_("0000020006040600080000000000", "", "wk1", "Lotus 1-2-3 spreadsheet (v1) file"),
    wk3("00001A0000100400", "", "wk3", "Lotus 123 spreadsheet (v3) file"),
    wk4("00001A0002100400", "", "wk4", "Lotus 1-2-3 spreadsheet (v4, v5) file"),
    wk5("00001A0002100400", "", "wk4", "Lotus 1-2-3 spreadsheet (v4, v5) file"),
    wks("2000604060", "", "wks", "Lotus 123 v1 Worksheet"),
    wks_("0E574B53", "", "wks", "DeskMate Worksheet"),
    wma("3026B2758E66CF11A6D900AA0062CE6C", "", "wma", "Microsoft Windows Media Audio/Video File(Advanced Streaming Format)"),
    wmf("01000900", "", "wmf", "Graphics Metafile"),
    wmf_("010009000003", "", "wmf", "Windows Metadata file (Win 3.x format)"),
    wmf__("02000900", "", "wmf", "Graphics Metafile"),
    wmf___("D7CDC69A", "", "wmf", "Windows Meta File"),
    wmv("3026B2758E66CF11A6D900AA0062CE6C", "", "wmv", "Microsoft Windows Media Audio/Video File(Advanced Streaming Format)"),
    wp("FF575043", "", "wp", "WordPerfect v5 or v6"),
    wpd("FF575043", "", "wpd", "WordPerfect"),
    wpg("FF575047", "", "wpg", "WordPerfect Graphics"),
    wri("31BE", "", "wri", "Microsoft Write file"),
    wri_("32BE", "", "wri", "Microsoft Write file"),
    ws("1D7D", "", "ws", "WordStar Version 5.0/6.0 document"),

    x32("4D5A", "", "x32", "Executable File"),
    xdr("3C", "", "xdr", "BizTalk XML-Data Reduced Schema file"),
    xla("D0CF11E0", "", "xla", "MS Office/OLE2"),
    xla_("D0CF11E0A1B11AE1", "", "xla", "MS Compound Document v1 or Lotus Approach APR file"),
    xls("D0CF11E0A1B11AE1", "", "xls", "MS Compound Document v1 or Lotus Approach APR file"),
    xls_("0902060000001000B9045C00", "", "xls", "MS Excel v2"),
    xls__("0904060000001000F6055C00", "", "xls", "MS Excel v4"),
    xls___("D0CF11E0", "", "xls", "MS Excel"),
    xls____("0908100000060500", "", "xls", "Excel spreadsheet subheader (MS Office)"),
    xlsx("504B030414000600080000002100", "", "xlsx", ""),
    xlt("D0CF11E0A1B11AE1", "", "xlt", "MS Compound Document v1 or Lotus Approach APR file"),
    xml("3C3F786D6C", "", "xml", "XML Document"),
    xml_("FFFE3C0052004F004F0054005300540055004200", "", "xml", "XML Document (ROOTSTUB)"),
    xul("7273696F6E3D22313C3F786D6C2076652E30223F3E", "", "xul", "XML User Interface Language file"),

    z("1F9D", "", "z", "TAR Compressed Archive File"),
    zip("504B0304", "", "zip", "ZIP Archive"),
    zip_("504B3030", "", "zip", "ZIP Archive (outdated)"),
    zipx("504B0304", "", "zipx", "ZIP Archive"),
    zoo("5A4F4F20", "", "zoo", "ZOO Archive File");

    public static final List<FileType> IMAGE_FILETYPES = Collections.unmodifiableList(Arrays.asList(bmp, gif, jpg, jpeg, png));
    public static final List<FileType> FLASH_FILETYPES = Collections.unmodifiableList(Arrays.asList(swf, flv));
    public static final List<FileType> MEDIA_FILETYPES = Collections.unmodifiableList(Arrays.asList(swf, flv, mp3, wav, wma, mid, avi, mpg, asf, rm, rmvb));
    public static final List<FileType> VIDEO_FILETYPES = Collections.unmodifiableList(Arrays.asList(mp4, avi, rmvb));
    public static final List<FileType> DEFAULT_FILETYPES = Collections.unmodifiableList(Arrays.asList(
            bmp, gif, jpg, jpeg, png, // 图片
            doc, docx, xls, xlsx, ppt, pptx, html, htm, txt, // word excel powerpoint
            rar, zip, gz, bz2, // 压缩文件
            mp4, avi, rmvb, // 视频格式
            pdf // pdf
    ));

    private final String hexHead; // 文件头十六进制字符串

    private final String mimePrefix; // mime 类型 字符串 前缀名

    private final String postFix; // 后缀名

    private final String desc; // 描述

    public boolean isJpg() {
        return (this == jpg
                || this == jpg_
                || this == jpg__
                || this == jpe
                || this == jpe_
                || this == jpeg
                || this == jpeg_
        );
    }

    public boolean isPng() {
        return (this == png
                || this == png_
                || this == PNG
                || this == PNG_
        );
    }

    public boolean isBmp() {
        return (this == bmp
                || this == BMP
        );
    }

    public boolean isHtml() {
        return (this == html
                || this == html_
                || this == html__
                || this == html___
                || this == htm
                || this == htm_
                || this == htm__
        );
    }

    /**
     * 获取文件头类型
     *
     * @param file
     * @return 文件头十六进制字符串
     */
    public static List<FileType> gainList(MultipartFile file) {
        String value = null;
        List<FileType> fileTypes = new ArrayList<>();
        try (InputStream iStream = file.getInputStream()) {
            byte[] bytes = new byte[3];
            iStream.read(bytes);
            value = ByteUtil.bytesToHexString(bytes);
            if (value == null) return null;

            for (FileType fileType : FileType.values()) {
                if (fileType.hexHead.length() < 6) {
                    if (fileType.hexHead.equals(value.substring(0, fileType.hexHead.length()))) {
                        fileTypes.add(fileType);
                    }
                } else {
                    if (fileType.hexHead.substring(0, 6).equals(value.substring(0, 6))) {
                        fileTypes.add(fileType);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileTypes;
    }

    @Nullable
    public static FileType gain(MultipartFile file) {
        String value = null;
        try (InputStream iStream = file.getInputStream()) {
            byte[] bytes = new byte[40];
            iStream.read(bytes);
            value = ByteUtil.bytesToHexString(bytes);
            if (value == null) return null;

            for (FileType fileType : FileType.values()) {
                if (fileType.hexHead.equals(value.substring(0, fileType.hexHead.length()))) {
                    return fileType;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
